package com.inews.business.web;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.inews.base.exception.BusinessException;
import com.inews.base.mapper.JsonMapper;
import com.inews.base.utils.InewsUtils;
import com.inews.base.utils.Servlets;
import com.inews.base.vo.ExtData;
import com.inews.business.service.DegreeStudyService;
import com.inews.business.vo.DegreeStudyVo;
import com.inews.business.vo.TeacherVo;

@Controller
@RequestMapping("/degreeStudy")
public class DegreeStudyController {
	
	@Autowired
	private DegreeStudyService service;
	
	
	/**
	 * 分页查询学历进修信息.
	 *
	 * @param request the request
	 * @param pageNumber the page number
	 * @param sort the sort
	 * @return the object
	 */
	@RequestMapping("findDegreeStudyPage")
	@ResponseBody
	public Object findDegreeStudyPage(ServletRequest request, @RequestParam(value = "page", defaultValue = "1") int pageNumber, String sort, TeacherVo vo){
		Pageable page = Servlets.buildPageRequest(pageNumber, sort);
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		ExtData<DegreeStudyVo> extData = service.findDegreeStudyPage(searchParams, page, vo);
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	/**
	 * 保存学历进修信息.
	 *
	 * @param vo the vbo
	 * @return the object
	 */
	@RequestMapping("saveDegreeStudy")
	@ResponseBody
	public Object saveDegreeStudy(DegreeStudyVo vo){
		ExtData<DegreeStudyVo> extData = service.saveDegreeStudy(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	/**
	 * 更新学历进修信息.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("updateDegreeStudy")
	@ResponseBody
	public Object updateDegreeStudy(DegreeStudyVo vo){
		ExtData<DegreeStudyVo> extData = service.updateDegreeStudy(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	/**
	 * 删除学历进修信息.
	 *
	 * @param vo the vo
	 * @return the object
	 */
	@RequestMapping("deleteDegreeStudy")
	@ResponseBody
	public Object deleteDegreeStudy(DegreeStudyVo vo){
		ExtData<DegreeStudyVo> extData = service.deleteDegreeStudy(vo, InewsUtils.getUserInfo());
		String json = JsonMapper.nonEmptyMapper().toJson(extData);
		return json;
	}
	
	
	
	/**
	 * 报表导出.
	 *
	 * @return the model and view
	 */
	@RequestMapping("exportDegreeStudyExcel")
	public String exportDegreeStudyExcel(){
		return "degreeStudyExcelView";
	}
	
	
	/**
	 * 报表导入.
	 *
	 * @return the model and view
	 */
	@RequestMapping("importExcel")
	@ResponseBody
	public String importExcel(@RequestParam("excelFile") MultipartFile excelFile){
		InputStream is;
		try {
			is = excelFile.getInputStream();
			ExtData<String> extData = service.importExcel(is, InewsUtils.getUserInfo());
			String json = JsonMapper.nonEmptyMapper().toJson(extData);
			return json;
		} catch (IOException e) {
			throw new BusinessException("导入失败");
		}
	}

}
